<?php

namespace App\Console\Commands;

use App\Model\Device;
use App\Model\DeviceOffLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class DeviceStatusCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'device:status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $list = Device::query()->where('read_at', '<', now()->subMinutes(20)->toDateTimeString())
            ->where('net_type', 1)
            ->where('online_status',1)
            ->get()->toArray();
        foreach($list as $v){
            $save['sn'] = $v['sn'];
            $save['device_id'] = $v['id'];
            DeviceOffLog::query()->create($save);
        }

        Device::query()->where('read_at', '<', now()->subMinutes(20)->toDateTimeString())
            ->where('net_type', 1)->update([
                'online_status' => 2,
                'signal' => 0,
                'volt' => 0,
                'curr' =>0,
                'power'=>0
                //'net_type' => 2
            ]);
        Device::query()->where('read_at', '<', now()->subMinutes(20)->toDateTimeString())
            ->where('net_type', 1)->where('is_master', 0)
            ->update([
                'net_type' => 2,
                'online_status'=>2
            ]);

    }
}
